package com.easy.modules.sysmgr.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.easy.common.dao.IBaseDao;
import com.easy.common.util.BeanUtils;
import com.easy.common.util.string.ClobUtil;
import com.easy.common.web.page.Msg;
import com.easy.modules.sysmgr.entity.PositionEntity;
import com.easy.modules.sysmgr.service.PositionServiceI;
import com.easy.modules.sysmgr.web.form.PositionForm;

@Service
@Transactional
public class PositionServiceImpl implements PositionServiceI {

	@Autowired
	private IBaseDao<PositionEntity> basedaoPosition;

	@Override
	public Msg save(PositionForm form) {
		try {
			PositionEntity entity = new PositionEntity();
			BeanUtils.copyNotNullProperties(form, entity, new String[]{"remark"});
			entity.setRemark(ClobUtil.getClob(form.getRemark())) ;
			entity.setCreated(new Date()) ; 

			if (form.getPid() != null && !"".equalsIgnoreCase(form.getPid())) {
				entity.setPosition(this.basedaoPosition.load(PositionEntity.class, form.getPid()));
			}
			this.basedaoPosition.add(entity);
			
			return new Msg(true, "创建成功!");
		} catch (BeansException e) {
			return new Msg(false, "创建失败！") ;
		}
	}
	
	@Override
	public Msg delete(PositionForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					PositionEntity t = this.basedaoPosition.load(PositionEntity.class, id);
					del(t);
				}
				return new Msg(true, "删除成功！") ;
			} else {
				return new Msg(false, "删除失败！") ;
			}
		} catch (BeansException e) {
			return new Msg(false, "删除失败！") ;
		}
	}
	
	private void del(PositionEntity entity) {
		if (entity.getPositions() != null && entity.getPositions().size() > 0) {
			for (PositionEntity r : entity.getPositions()) {
				del(r);
			}
		}
		this.basedaoPosition.delete(PositionEntity.class, entity.getId());
	}

	@Override
	public Msg edit(PositionForm form) {
		try {
			PositionEntity entity = this.basedaoPosition.load(PositionEntity.class, form.getId());
			
			if(entity != null) {
				BeanUtils.copyNotNullProperties(form, entity, new String[]{"remark"});
				entity.setModifyDate(new Date()) ;
				entity.setRemark(ClobUtil.getClob(form.getRemark())) ;
				
				if (form.getPid() != null && !form.getPid().equalsIgnoreCase("")) {
					entity.setPosition(basedaoPosition.load(PositionEntity.class, form.getPid()));
				}
				if (form.getPid() != null && !form.getPid().equalsIgnoreCase("")) {
					PositionEntity pt = basedaoPosition.load(PositionEntity.class, form.getPid());
					isChildren(entity, pt);
					entity.setPosition(pt);
				} else {
					entity.setPosition(null);
				}
			}
			
			return new Msg(true, "编辑成功！") ;
		} catch (BeansException e) {
			return new Msg(false, "编辑失败！") ;
		}
	}
	
	/**
	 * 判断是否是将当前节点修改到当前节点的子节点
	 * @param entity 当前节点
	 * @param pt 要修改到的节点
	 * @return
	 */
	private boolean isChildren(PositionEntity entity, PositionEntity pt) {
		if (pt != null && pt.getPosition() != null) {
			if (pt.getPosition().getId().equalsIgnoreCase(entity.getId())) {
				pt.setPosition(null);
				return true;
			} else {
				return isChildren(entity, pt.getPosition());
			}
		}
		return false;
	}
	

	@Override
	public PositionForm get(PositionForm form) {

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", form.getId());
		PositionEntity entity = (PositionEntity) this.basedaoPosition.queryObject("select t from PositionEntity t where t.id = :id", params);
		
		PositionForm pf = new PositionForm();
		
		if(null != entity) {
			BeanUtils.copyNotNullProperties(entity, pf, new String[]{"remark"});
			pf.setRemark(ClobUtil.getString(entity.getRemark())) ;
			
			if (entity.getPosition() != null) {
				pf.setPid(entity.getPosition().getId());
			}
		}
		return pf ;
	}

	@Override
	public List<PositionForm> tree(PositionForm form) {
		String hql = "select t from PositionEntity t where t.position is null order by created desc" ;
		
		List<PositionEntity> entitys = this.basedaoPosition.list(hql) ;
		
		List<PositionForm> companysform = new ArrayList<PositionForm>() ;
		
		for (PositionEntity PositionEntity : entitys) {
			companysform.add(recursiveNode(PositionEntity)) ;
		}
		return companysform ;
	}
	
	public PositionForm recursiveNode(PositionEntity entity) {
		PositionForm mf = new PositionForm() ;
		BeanUtils.copyNotNullProperties(entity, mf, new String[]{"remark"}) ;
		
		mf.setRemark(ClobUtil.getString(entity.getRemark())) ;
		
		mf.setText(entity.getName()) ;	
		
		if(null != entity.getPositions() && entity.getPositions().size() > 0) {
			mf.setState("closed") ;
			
			Set<PositionEntity> rs = entity.getPositions() ;
			List<PositionForm> children = new ArrayList<PositionForm>();
			for (PositionEntity PositionEntity : rs) {
				
				PositionForm tn = recursiveNode(PositionEntity) ;
				BeanUtils.copyNotNullProperties(PositionEntity ,tn, new String[]{"remark"}) ;
				tn.setRemark(ClobUtil.getString(PositionEntity.getRemark())) ;
				children.add(tn);
			}
			
			mf.setChildren(children) ;
			mf.setState("open");
		}
		return mf ;
	}

}
